from langchain_community.chat_message_histories import SQLChatMessageHistory

def get_message_history(session_id: str) -> SQLChatMessageHistory:
    return SQLChatMessageHistory(
        session_id=session_id,
        connection_string="sqlite:///message_history.db",
        table_name="message_history",
    )

from langchain_core.messages import HumanMessage
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_deepseek import ChatDeepSeek
from langchain.schema.output_parser import StrOutputParser

llm = ChatDeepSeek(model="deepseek-chat", temperature=0)

runnable = llm | StrOutputParser()

runnable_with_message_history = RunnableWithMessageHistory(
    runnable, # 可运行的对象
    get_message_history, # 获取消息历史
)

ret = runnable_with_message_history.invoke(
    [HumanMessage(content="你好，我是小明")],
    config={"configurable": {"session_id": "xiaoming"}},
)

print(ret)

ret = runnable_with_message_history.invoke(
    [HumanMessage(content="你知道我是谁吗？")],
    config={"configurable": {"session_id": "xiaoming"}},
)

print(ret)



